package com.healthcare.admin.controller;


import com.healthcare.admin.service.GroupService;
import com.healthcare.admin.service.ReferralService;
import com.healthcare.admin.utils.DateUtil;
import com.healthcare.admin.vo.AdminStatisticsDailyVo;
import com.healthcare.common.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 * 会诊申请 前端控制器
 * </p>
 *
 * @author 新华医院转诊联盟
 * @since 2020-04-12
 */
@Api(tags = "统计管理")
@RestController
@RequestMapping("/admin/group")
public class GroupController {
    @Autowired
    private GroupService groupService;

    @Autowired
    private ReferralService referralService;

    @GetMapping("show-chart")
    @ApiOperation(value = "查询")
    public Result showChart(
            @ApiParam(name = "type",value = "会转诊类型") @RequestParam(required = true) Integer type,
            @ApiParam(name = "begintime",value = "开始时间") @RequestParam(required = true) String begintime,
            @ApiParam(name = "endtime",value = "结束时间") @RequestParam(required = true) String endtime
                           ) throws ParseException, ParseException {
        List<AdminStatisticsDailyVo> create = new ArrayList<>();
        List<AdminStatisticsDailyVo> group =new ArrayList<>();
        List<AdminStatisticsDailyVo> refuse=new ArrayList<>();
        List<AdminStatisticsDailyVo> finish=new ArrayList<>();
        List<String> date = DateUtil.dateriqi(begintime,endtime);
        if (type==1){ // 会诊
            create= groupService.selectGmtTime(begintime,endtime);//求诊量
            group=  groupService.selectGroupTime(begintime,endtime);//分诊量
            refuse= groupService.selectRefusedTime(begintime,endtime);//拒绝量
            finish = groupService.selectSuccessfulTime(begintime,endtime);//会诊量
        }else {  //转诊
            create= referralService.selectGmtcreateTime(begintime,endtime);//申请量
            group=  referralService.selectGroupTime(begintime,endtime);//审核量
            refuse= referralService.selectRefusedTime(begintime,endtime);//拒绝量
            finish = referralService.selectSuccessfulTime(begintime,endtime);//就诊量
        }

        List<String> cre = new ArrayList<>();
        List<String> gro = new ArrayList<>();
        List<String> ref = new ArrayList<>();
        List<String> fin = new ArrayList<>();

        List<String> timee = new ArrayList<>();
        for (int i = 0; i <date.size() ; i++) {
            timee.add(date.get(i));
        }
        for (int i = 0; i <create.size() ; i++) {
            cre.add(create.get(i).getTime());
        }
        timee.removeAll(cre);
        for (int i = 0; i <timee.size() ; i++) {
            AdminStatisticsDailyVo adminStatisticsDailyVo = new AdminStatisticsDailyVo();
            adminStatisticsDailyVo.setTime(timee.get(i));
            adminStatisticsDailyVo.setQuantity(0);
            create.add(adminStatisticsDailyVo);
        }
        for (int i = 0; i <group.size() ; i++) {
            gro.add(group.get(i).getTime());
        }
        timee.clear();
        for (int i = 0; i <date.size() ; i++) {
            timee.add(date.get(i));
        }
        timee.removeAll(gro);
        for (int i = 0; i <timee.size() ; i++) {
            AdminStatisticsDailyVo adminStatisticsDailyVo = new AdminStatisticsDailyVo();
            adminStatisticsDailyVo.setTime(timee.get(i));
            adminStatisticsDailyVo.setQuantity(0);
            group.add(adminStatisticsDailyVo);
        }
        for (int i = 0; i <refuse.size() ; i++) {
            ref.add(refuse.get(i).getTime());
        }
        timee.clear();
        for (int i = 0; i <date.size() ; i++) {
            timee.add(date.get(i));
        }
        timee.removeAll(ref);
        for (int i = 0; i <timee.size() ; i++) {
            AdminStatisticsDailyVo adminStatisticsDailyVo = new AdminStatisticsDailyVo();
            adminStatisticsDailyVo.setTime(timee.get(i));
            adminStatisticsDailyVo.setQuantity(0);
            refuse.add(adminStatisticsDailyVo);
        }
        for (int i = 0; i <finish.size() ; i++) {
            fin.add(finish.get(i).getTime());
        }
        timee.clear();
        for (int i = 0; i <date.size() ; i++) {
            timee.add(date.get(i));
        }
        timee.removeAll(fin);
        for (int i = 0; i <timee.size() ; i++) {
            AdminStatisticsDailyVo adminStatisticsDailyVo = new AdminStatisticsDailyVo();
            adminStatisticsDailyVo.setTime(timee.get(i));
            adminStatisticsDailyVo.setQuantity(0);
            finish.add(adminStatisticsDailyVo);
        }
        ListSort(create);
        ListSort(group);
        ListSort(refuse);
        ListSort(finish);

        List<Integer> c = new ArrayList<>();
        List<Integer> g = new ArrayList<>();
        List<Integer> r = new ArrayList<>();
        List<Integer> f = new ArrayList<>();
        for (int i = 0; i <create.size() ; i++) {
            c.add(create.get(i).getQuantity());
        }
        for (int i = 0; i <group.size() ; i++) {
            g.add(group.get(i).getQuantity());
        }
        for (int i = 0; i <refuse.size() ; i++) {
            r.add(refuse.get(i).getQuantity());
        }
        for (int i = 0; i <finish.size() ; i++) {
            f.add(finish.get(i).getQuantity());
        }
        return Result.ok().data("dater",date).data("create",c).data("group",g).data("refuse",r).data("finish",f);

    }

    private static void ListSort(List<AdminStatisticsDailyVo> list) {
        Collections.sort(list, new Comparator<AdminStatisticsDailyVo>() {
            @Override
            public int compare(AdminStatisticsDailyVo o1, AdminStatisticsDailyVo o2) {
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                try {
                    Date dt1 = format.parse(o1.getTime());
                    Date dt2 = format.parse(o2.getTime());
                    if (dt1.getTime() > dt2.getTime()) {
                        return 1;
                    } else if (dt1.getTime() < dt2.getTime()) {
                        return -1;
                    } else {
                        return 0;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return 0;
            }
        });
    }

}

